#include "login.h"
#include "ui_login.h"
#include "global.h"

#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>

login::login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    connect(ui->loginbtn,SIGNAL(clicked(void)),
            this,SLOT(loginok(void)));
    ui->stu_btn->setChecked(1);
}

login::~login()
{
    delete ui;
}

void login::loginok(){
    QString user;
    QString pwd;
    user=ui->usernameEdit->text();
    pwd=ui->pwdEdit->text();

    //获取单选按钮状态
    QString status;
    if( ui->stu_btn->isChecked()) status="student";
    else if(ui->manage_btn->isChecked()) status="guanliyuan";
    else if(ui->suguan_btn->isChecked()) status="housemaster";

    //根据状态选择合适的属性
    QString q1,q2;
    if(status=="student") q1="Sid",q2="Spwd";
    if(status=="guanliyuan") q1="Gid",q2="Gpwd";
    if(status=="housemaster") q1="Hid",q2="Hpwd";

    QString sql=QString("select * from %1 where %2='%3' and %4='%5' ").arg(status).arg(q1).arg(user).arg(q2).arg(pwd);
    QSqlQuery query;
//    qDebug()<<sql;
    query.exec(sql);
    if(query.next()){
//        student.show();
        qDebug()<<"成功登录";
        this->close();
        global::userid=query.value(0).toString();
        qDebug()<<global::userid;
        if(status=="student"){
            studentInedx window;
            window.exec();
        }
        else if(status=="guanliyuan"){
            manageIndex window;
            window.exec();
        }
        else {
            houseMasterIndex window;
            window.exec();
        }

    }
    else  QMessageBox::warning(NULL,"Error","用户名或密码错误，请注意用户类型是否选对！！！");
}
